let db = require("../db/db")
let handler = require("./data_model_service_handler");
let custom = require("./report/custom")

let handle = async function (action, params, login_info, req, res) {
    switch (action) {
        case "datasources":
            {
                let sql = "select * from sys_datamodel where is_deleted=0"
                let r = await db.query(sql, []);
                let custom_result = {
                    label: "自定义数据源",
                    children: []
                }
                for (let key of Object.keys(custom.services)) {
                    let service = custom.services[key];
                    let dss = service.getdatasources();
                    for (let ds of dss) {
                        custom_result.children.push({
                            label: ds.label,
                            value: "custom:" + key + ":" + ds.name
                        })
                    }
                }
                let datamodel_result = {
                    label: "数据模型",
                    children: []
                }
                for (let row of r.rows) {
                    datamodel_result.children.push({
                        label: row.name,
                        value: "datamodel:" + row.id + ":" + row.tablename
                    })
                }
                return [custom_result, datamodel_result];
                break;
            }
        case "getcolumns":
            {
                let datasource = params.datasource;
                let type = datasource.split(":")[0];
                if (type == "datamodel") {
                    let datamodel = datasource.split(":")[1];
                    let tablename = datasource.split(":")[2];
                    let result = await handler.getinfo({ datamodel, tablename }, login_info, req, res);
                    let columns = [];
                    for (let f of result.fields) {
                        columns.push({
                            label: f.label + " (" + f.name + ")",
                            value: f.name + ":" + f.label
                        })
                    }
                    return columns;
                }
                else if (type == "custom") {
                    let key = datasource.split(":")[1];
                    let datasource_name = datasource.split(":")[2];
                    let service = custom.services[key];
                    let cols = service.getcolumns(datasource_name);
                    let columns = [];
                    for (let c of cols) {
                        columns.push({
                            label: c.label + " (" + c.name + ")",
                            value: c.name + ":" + c.label
                        })
                    }
                    return columns;
                }
                break;
            }
        case "getfilterfields":
            {
                let datasource = params.datasource;
                let type = datasource.split(":")[0];
                if (type == "datamodel") {
                    let datamodel = datasource.split(":")[1];
                    let tablename = datasource.split(":")[2];
                    let result = await handler.getinfo({ datamodel, tablename }, login_info, req, res);
                    let columns = [];
                    for (let f of result.fields) {
                        if (f.type == "date" || f.type == "datetime" || f.type == "time" || f.type == "int") {
                            columns.push({
                                label: f.label + " (" + f.name + ")",
                                name: f.name,
                                type: f.type == "int" ? "number" : f.type,
                                operators: ['equal', 'not_equal', 'less', 'greater', 'between']
                            })
                            columns.push({ //非文本类型的过滤值需要输入关联到搜索框的值${}形式，这种通过一个新的字段条目来让用户输入，字段名前加*区分
                                label: f.label + "[含变量] (" + f.name + ")",
                                name: "*"+f.name,
                                type: "text",
                                operators: ['equal', 'not_equal', 'less', 'greater', 'between']
                            })
                        }
                        else {
                            columns.push({
                                label: f.label + " (" + f.name + ")",
                                name: f.name,
                                type: "text",
                                operators: ['equal', 'not_equal', 'like', 'not_like']
                            })
                        }
                    }
                    return columns;
                }
                else if (type == "custom") {
                    let key = datasource.split(":")[1];
                    let datasource_name = datasource.split(":")[2];
                    let service = custom.services[key];
                    let cols = service.getfilterfields(datasource_name);
                    let columns = [];
                    for (let f of cols) {
                        if (f.type == "date" || f.type == "datetime" || f.type == "time" || f.type == "number") {
                            columns.push({
                                label: f.label + " (" + f.name + ")",
                                name: f.name,
                                type: f.type,
                                operators: f.operators
                            })
                            columns.push({ //非文本类型的过滤值需要输入关联到搜索框的值${}形式，这种通过一个新的字段条目来让用户输入，字段名前加*区分
                                label: f.label + "[含变量] (" + f.name + ")",
                                name: "*"+f.name,
                                type: "text",
                                operators: f.operators
                            })
                        }
                        else {
                            columns.push({
                                label: f.label + " (" + f.name + ")",
                                name: f.name,
                                type: "text",
                                operators: f.operators
                            })
                        }
                    }
                    return columns;
                }
                break;
            }
        case "getdata":
            {
                let datasource = params.datasource;
                let filter = params.filter;
                let limit = params.limit;
                let type = datasource.split(":")[0];
                if (type == "datamodel") {
                    let datamodel = datasource.split(":")[1];
                    let tablename = datasource.split(":")[2];
                    let result = await handler.getrows({ datamodel, tablename, filter: filter ? [filter] : filter ,limit}, login_info, req, res);
                    return result;
                }
                else if (type == "custom") {
                    let key = datasource.split(":")[1];
                    let datasource_name = datasource.split(":")[2];
                    let service = custom.services[key];
                    let data = service.getdata(datasource_name, filter, limit);
                    return data;
                }
                break;
            }
        default:
            return Error('未知的action:' + action);
    }
}

module.exports = { handle };